package com.example.newproject_overseas.ViewModel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.navigation.NavHostController
import com.example.newproject_overseas.exception.NetworkExceptionHandler
import com.example.newproject_overseas.model.entity.ModelItem
import com.example.newproject_overseas.model.entity.Province
import com.example.newproject_overseas.model.service.base.AddressServices
import com.example.newproject_overseas.model.service.base.JobStationServices
import kotlinx.coroutines.flow.MutableStateFlow
import androidx.compose.runtime.remember as remember1

class JobViewModel: ViewModel() {
    //工作岗位接口
    private val JobStationService = JobStationServices.instance();

    var model: List<Province>? = null;
    suspend fun JobStationReq(navController: NavHostController){
        try {
            val cenobitismResp = JobStationService.bitcheryShoelessKheda()
            if (cenobitismResp.status == 0){
                model = cenobitismResp.model;

            }
            // 处理正常的响应逻辑
        } catch (e: Exception) {
            Log.i("失败了",e.toString())
            NetworkExceptionHandler.handleException(e,navController)
        }
    }

    //获取地址信息接口
    //接口定义
    private val addressService = AddressServices.instance()
    //数据赋值
    var addressModel: List<ModelItem>? = null;
    // 添加标志位用于判断是否是第一次请求
    var isFirstRequest = true
    suspend fun addressReq(navController: NavHostController,parentId: String? = null){
        try {
            val request = if (isFirstRequest) {
                isFirstRequest = false
                mapOf("model" to "") // 第一次请求，传递空的父级 Id
            } else {
                mapOf("model" to parentId) // 第二次请求，传递具体的父级 Id
            }
            val response = addressService.mortonPropulsionBalky(request) // 第二次请求，传递参数
            if (response.status == 0) {
                var fatherData = response.model
                Log.i("addressReq: ",fatherData.toString())

                // 遍历并处理数据
//                for (item in fatherData!!) {
//                    if (item.haveChild) {
//                        val childrenParentId = item.id
//                        addressReq2(navController, childrenParentId,item)
//                    }
//                }
                addressModel = fatherData;
                Log.i( "777777: ", addressModel.toString(),)
            }
        } catch (e: Exception) {
            // Handle exception
            Log.i("add失败了",e.toString())
            NetworkExceptionHandler.handleException(e,navController)
        }
    }


    //获取2级地址信息
    //数据赋值
    val addressOneId = MutableStateFlow<String?>(null)
    var addressChilderModel: List<ModelItem>? = null;
    suspend fun addressReq2(navController: NavHostController,parentId: String){
        try {
            Log.i("11234", "addressReq2: ")
            val request =  mapOf("model" to parentId) // 第二次请求，传递具体的父级 Id
            val response = addressService.mortonPropulsionBalky(request) // 第二次请求，传递参数
            Log.i("66666", "addressReq2: ")
            if (response.status == 0) {
                var childrenModel = response.model
                // 将子级数据添加到父级的 children 属性中
//                parentItem.children = childrenModel;
                addressChilderModel = childrenModel;
                Log.i( "addressReq2: ",addressChilderModel.toString())
            }
        } catch (e: Exception) {
            // Handle exception
            Log.i("add失败了",e.toString())
            NetworkExceptionHandler.handleException(e,navController)
        }
    }





}